﻿using JWT.Builder;
using ManYan.Application;
using ManYan.Common;
using ManYan.Common.jwt;
using ManYan.Domain.Model.Entity;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace ManYan.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class BaseController<T, TVO> : ControllerBase where T : class, new() where TVO : class, new()
    {
        protected Guid UserId;
        protected string UserNickName;
        protected string? Roles;
        protected readonly IBaseAppService<T> _baseAppService;
        private readonly IHttpContextAccessor _httpContextAccessor;
        public BaseController(IBaseAppService<T> baseAppService, IHttpContextAccessor httpContextAccessor)
        {
            //先在构造函数里拿到客户端传来的token,然后对token进行解密
            _baseAppService = baseAppService;
            _httpContextAccessor = httpContextAccessor;
            var header = _httpContextAccessor?.HttpContext?.Request.Headers;
            if (header!=null&&header.ContainsKey("Authorization"))
            {
                var token = header["Authorization"].ToString().Replace("Bearer ", "");
                JwtHelper jwtHelper = new JwtHelper();
                dynamic res = JObject.Parse(jwtHelper.DecodeJwt(token));
                this.UserId = res?.user_id;
                this.Roles = res?.role;



            }
        }
        [HttpGet]
        public virtual IEnumerable<T> Get()
        {
            throw new Exception("");
        }
        // GET: api/<BaseController> Res<List<tb_exam_content>>
        [HttpPost]
        public virtual Res<List<T>> Get(TVO t)
        {
            throw new Exception("");
        }

        // GET api/<BaseController>/5
        [HttpGet("{id}")]
        public  Res Get(Guid id)
        {

            ParameterExpression parameterExpression = Expression.Parameter(typeof(T), "c");
            //c.Age > 35
            ConstantExpression constantId = Expression.Constant(id);
            PropertyInfo? propId = typeof(T).GetProperty("Id");
            if (propId != null)
            {
                MemberExpression idExp = Expression.Property(parameterExpression, propId);
                BinaryExpression cagExp = Expression.Equal(idExp, constantId);
                Expression<Func<T, bool>> predicate1 = Expression.Lambda<Func<T, bool>>(cagExp, parameterExpression);
                return _baseAppService.WhereLoadEntity(predicate1);
            }

            // System.Linq.Expressions.Expression<Func<tb_exam_content, bool>> expression = x => x.Id == id;
            throw new Exception("");
            //  return _baseAppService.WhereLoadEntity(expression);
        }

        // POST api/<BaseController>
        [HttpPost]
        public Res Post(T value)
        {
            return _baseAppService.AddEntity(value);
        }

        // PUT api/<BaseController>/5
       [HttpPut("{id}")]
        public virtual Res Put(Guid Id,T value)
        {
         
            throw new Exception("");
        }

        // DELETE api/<BaseController>/5
        [HttpDelete("{id}")]
        public int Delete(Guid id)
        {
            ParameterExpression parameterExpression = Expression.Parameter(typeof(T), "c");
            //c.Age > 35
            ConstantExpression constantId = Expression.Constant(id);
            PropertyInfo? propId = typeof(T).GetProperty("Id");
            if (propId != null)
            {
                MemberExpression idExp = Expression.Property(parameterExpression, propId);
                BinaryExpression cagExp = Expression.Equal(idExp, constantId);
                Expression<Func<T, bool>> predicate1 = Expression.Lambda<Func<T, bool>>(cagExp, parameterExpression);
                return _baseAppService.DelEntity(predicate1);
            }
            else
                return 0;
        }
    }
}
